﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using VehicleManagement.Codes;
using VehicleManagement.Models;
using System.Collections;
using VehicleManagement.Controllers;

namespace VehicleManagement.ControllersServices
{
    public class CarInsureServices
    {

        CarInfoServices carServices = new CarInfoServices();
        /// <summary>
        /// 根据车ID查车牌号
        /// </summary>
        /// <param name="approval">车辆保险表信息对象</param>
        /// <returns>修改后的车辆保险表信息对象</returns>
        public CarInsure GetDriverNameAndCarNumber(ref CarInsure carinsure)
        {
            carinsure.CarNumber = carServices.GetCarNumByCarId(carinsure.CarID);
            return carinsure;
        }
        /// <summary>
        /// 根据车牌号查车ID
        /// </summary>
        /// <param name="approval">车辆保险表信息对象</param>
        /// <returns>修改后的车辆保险表信息对象</returns>
        public CarInsure GetCarID(ref CarInsure carinsure)
        {
            carinsure.CarID = carServices.GetCarIdByCarNum(carinsure.CarNumber);
            return carinsure;
        }
        /// <summary>
        /// 模糊查找车辆保险信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>车辆保险信息对象</returns>
        
        public IList<CarInsure> SearchAllInsures(Hashtable ht)
        {
            IList<CarInsure> lstInsure = new List<CarInsure> { };
            lstInsure = SqlMapperManager.Instance.Get().QueryForList<CarInsure>("SEARCH_CarInsure", ht);
            return lstInsure;
        }

        public object SearchAllInsures(Hashtable ht, int skip, int max)
        {
            IList<CarInsure> lstInsure = new List<CarInsure> { };
            lstInsure = SqlMapperManager.Instance.Get().QueryForList<CarInsure>("SEARCH_CarInsure", ht, skip, max);
            return lstInsure;
        }
        ///// <summary>
        ///// 获取车辆保险信息
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns>车辆保险信息对象</returns>
        //public IList<CarInsure> GetAllCarinsure()
        //{
        //    IList<CarInsure> lstInsure = SqlMapperManager.Instance.Get().QueryForList<CarInsure>("SELECT_ALL_CarInsure", null);
        //    return lstInsure;
        //}


        /// <summary>
        /// 查找指定的保险单信息
        /// </summary>
        /// <param name="id">保险单ID</param>
        /// <returns>车保险信息对象</returns>
        public CarInsure GetInsuresByCarInsureID(int id)
        {
            IList<CarInsure> lstModel = SqlMapperManager.Instance.Get().QueryForList<CarInsure>("SELECT_ALL_CarInsure", null);
            CarInsure carinsure = lstModel.Where(u => u.CarInsureID == id).First();
            return carinsure;
        }

        /// <summary>
        /// 增加一个保险单
        /// </summary>
        /// <param name="id">保险表信息对象</param>
        /// <returns>所增加条目的主键</returns>
        public object Create(CarInsure insure)
        {
            GetCarID(ref insure);
            return SqlMapperManager.Instance.Get().Insert("INSERT_CarInsure", insure);
        }

        /// <summary>
        /// 修改一个保险单
        /// </summary>
        /// <param name="id">保险表</param>
        /// <returns>修改列数</returns>
        public int Edit(CarInsure insure)
        {
            GetCarID(ref insure);
            return SqlMapperManager.Instance.Get().Update("UPDATE_CarInsure", insure);
        }

        /// <summary>
        /// 删除一个保险单
        /// </summary>
        /// <param name="id">保险表ID</param>
        /// <returns>删除列数</returns>
        public int Delete(int id)
        {
            return SqlMapperManager.Instance.Get().Delete("DELETE_CarInsure", id);
        }



        public int CountAllInsure(Hashtable ht)
        {
            return SearchAllInsures(ht).Count;
        }

      
    }
}